package epam.marchenko.library.controller;

import java.util.Map;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import epam.marchenko.library.domain.BookClass;
import epam.marchenko.library.domain.User;
import epam.marchenko.library.services.BookService;
import epam.marchenko.library.services.UserService;

/**
 * Handles requests for the application home page.
 */
@Controller
public class GuestController {

	private static final Logger logger = LoggerFactory
			.getLogger(GuestController.class);

	BookService bookService;
	UserService userService;

	@Autowired
	public GuestController(BookService bookService, UserService userService) {
		this.bookService = bookService;
		this.userService = userService;
	}

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Model model) {
		logger.info("Welcome to main page!");

		Map<BookClass, Integer> books = bookService
				.getBooksWithAvailableCount();
		model.addAttribute("books", books);

		return "index";
	}

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login(Model model, @RequestParam(value="login_error", required = false)String isError) {
		logger.info("Login");
		model.addAttribute("loginError",isError);
		return "login";
	}

	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public String logout() {
		logger.info("Logout");
		return "logout";
	}

	@RequestMapping(value = "/registration", method = RequestMethod.GET)
	public String createUser(Model model) {
		logger.info("Creating User");
		model.addAttribute("user", new User());
		return "registration";
	}

	@RequestMapping(value = "/registration", method = RequestMethod.POST)
	public String addUserFromForm(@Valid User user, BindingResult bindingResult) {
		logger.info("Add User");
		if (bindingResult.hasErrors()) {
			return "registration";
		}

		if (!userService.isAvailableLogin(user.getLogin())) {
			bindingResult.addError(new FieldError(
					bindingResult.getObjectName(), "login",
					"Specified login is already taken."));
			return "registration";
		}

		userService.addUser(user);
		return "redirect:user/";
	} 

	@RequestMapping(value = "/error", method = RequestMethod.GET, params = "code")
	public String error(Model model, @RequestParam(value="code")Integer code) {
		logger.info("Some Error");
		model.addAttribute("error",code);
		return "error";
	}

}
